package com.example.tianxiaoyu.zyfypt__128txy.model;


import com.example.tianxiaoyu.zyfypt__128txy.bean.SpecialBean;
import com.example.tianxiaoyu.zyfypt__128txy.listener.SpecialListener;
import com.example.tianxiaoyu.zyfypt__128txy.iface.Specialiface;
import com.example.tianxiaoyu.zyfypt__128txy.service.SpecialService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Apple on 2017/4/19.
 */

public class SpecialModel implements Specialiface {
    private Retrofit retrofit;
    private String BASEURL="http://amicool.neusoft.edu.cn/";

    //构造方法
    public SpecialModel()
    {
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    //获取文章数据
    @Override
    public void getResultList(String mod, int page, String sessionID, final SpecialListener specialListener) {
         SpecialService specialService
                =retrofit.create(SpecialService.class);
        Call<List<SpecialBean>> call
                =specialService.getResult(mod,page,sessionID);
        call.enqueue(new Callback<List<SpecialBean>>() {
            @Override
            public void onResponse(Call<List<SpecialBean>> call, Response<List<SpecialBean>> response) {
                if(response.isSuccessful() && response.body()!=null)
                specialListener.onResponse(response.body());
            else
                    specialListener.onFail("on response fail");
          }
            @Override
            public void onFailure(Call<List<SpecialBean>> call, Throwable t) {
            specialListener.onFail(t.toString());
            }
        });
    }
}
